﻿using LiveCharts.Wpf;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using TempCtr.Model;
using Color = System.Drawing.Color;
using Series = System.Windows.Forms.DataVisualization.Charting.Series;

namespace TempCtr
{
    public partial class temp_variation : Form
    {
        public string Num;
        public int Id;
        public bool IsRead = false;

        public temp_variation(Thermostat th)
        {
            InitializeComponent();
            InitializeChart();
            Id = th.ID;
            Num = th.Num;
        }

        public void InitializeChart()
        {
            #region 设置图表的属性
            //图表的背景色
            chart1.BackColor = Color.FromArgb(211, 223, 240);
            //图表背景色的渐变方式
            chart1.BackGradientStyle = GradientStyle.None;
            //图表的边框颜色、
            chart1.BorderlineColor = Color.FromArgb(26, 59, 105);
            //图表的边框线条样式
            chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            //图表边框线条的宽度
            chart1.BorderlineWidth = 2;
            //图表边框的皮肤
            chart1.BorderSkin.SkinStyle = BorderSkinStyle.None;
            #endregion

            #region 设置图表的Title
            Title title = new Title();
            //标题内容
            title.Text = "温度曲线";
            //标题的字体
            title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Regular);
            //标题字体颜色      
            //title.ForeColor = Color.FromArgb(26, 59, 105);
            //标题阴影颜色
            //title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
            //标题阴影偏移量
            //title.ShadowOffset = 3;
            chart1.Titles.Add(title);
            #endregion

            #region 设置图表区属性
            //图表区的名字
            ChartArea chartArea = new ChartArea("Default");
            //背景色
            chartArea.BackColor = Color.White;// Color.FromArgb(64, 165, 191, 228);
            //背景渐变方式
            chartArea.BackGradientStyle = GradientStyle.None;
            //渐变和阴影的辅助背景色
            chartArea.BackSecondaryColor = Color.White;
            //边框颜色
            chartArea.BorderColor = Color.Blue;
            //边框线条宽度
            chartArea.BorderWidth = 2;
            //边框线条样式
            chartArea.BorderDashStyle = ChartDashStyle.Solid;
            //阴影颜色
            //chartArea.ShadowColor = Color.Transparent;

            //设置X轴和Y轴线条的颜色和宽度
            chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.LineWidth = 1;
            chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.LineWidth = 1;
            //设置X轴和Y轴的标题
            //chartArea.AxisX.Title = "time";
            //chartArea.AxisY.Title = "count";
            //chartArea.AxisX.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            //chartArea.AxisY.TitleFont = new System.Drawing.Font("Microsoft Sans Serif", 10, FontStyle.Regular);
            //设置图表区网格横纵线条的颜色和宽度
            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.MajorGrid.LineWidth = 1;
            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.MajorGrid.LineWidth = 1;
            chart1.ChartAreas.Add(chartArea);
            #endregion

            #region 图例及图例的位置
            Legend legend = new Legend();
            legend.Alignment = StringAlignment.Center;
            legend.Docking = Docking.Bottom;
            legend.BackColor = Color.Transparent;
            this.chart1.Legends.Add(legend);
            #endregion
        }

        private void temp_variation_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            label2.Text = Num;

            dateTimePicker1.Value = DateTime.Now.AddDays(-5);
            dateTimePicker2.Value = DateTime.Now;
            dateTimePicker1.MaxDate = DateTime.Now;
            dateTimePicker2.MaxDate = DateTime.Now;

            GetTempTable();
        }
        
        private void GetTempTable()
        {
            try
            {
                DataTable dataTable = MySqlHelp.GetTemp(Id, dateTimePicker1.Value, dateTimePicker2.Value);
                Series series = SetSeriesStyle();           
                chart1.Series.Clear();
                foreach (DataRow row in dataTable.Rows)
                {
                    series.Points.AddXY(row[3], row[1]);                  
                }
                this.chart1.Series.Add(series);
       
            }
            catch(Exception ex)
            {
               // MainFrom.Logger.Error("获取"+label2.Text+"历史数据失败"+ex.ToString());
            }
            
        }



      

     
        private Series SetSeriesStyle()
        {
            Series series = new Series();            //Series的类型
            series.ChartType = SeriesChartType.Line;            //Series的边框颜色
            series.BorderColor = Color.FromArgb(180, 26, 59, 105);            //线条宽度
            series.BorderWidth = 3;            //线条阴影颜色
            //series.ShadowColor = Color.Black;
            //阴影宽度
            //series.ShadowOffset = 2;
            //是否显示数据说明
            series.IsVisibleInLegend = false;            //线条上数据点上是否有数据显示
            series.IsValueShownAsLabel = false;            //线条上的数据点标志类型
           // series.MarkerStyle = MarkerStyle.Circle;            //线条数据点的大小
           // series.MarkerSize = 2;
            //线条颜色          
            series.Color = Color.FromArgb(220, 65, 140, 240); 
           
            return series;
        }

        private void dateTimePicker1_MouseDown(object sender, MouseEventArgs e)
        {
            DateTime dateTime = DateTime.Now;
            try
            {
                dateTimePicker1.MaxDate = dateTime;
                dateTimePicker1.Value = dateTime.AddDays(-1);
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("timepicker1 错误 " + ex.ToString());
            }
        }

        private void dateTimePicker2_MouseDown(object sender, MouseEventArgs e)
        {
            DateTime dateTime = DateTime.Now;
            try
            {
                dateTimePicker2.MaxDate = dateTime;
                dateTimePicker2.Value = dateTime;
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("timepicker2 错误 " + ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            GetTempTable();//baocuo
        }
    }
}
